home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / os2 / bind493a.zip / res / nsap_addr.c < prev    next >
C/C++ Source or Header  |  1995-06-29  |  2KB  |  97 lines

  1. #if defined(LIBC_SCCS) && !defined(lint)
  2. static char rcsid[] = "$Id: nsap_addr.c,v 8.2 1995/06/29 09:26:28 vixie Exp $";
  3. #endif /* LIBC_SCCS and not lint */
  4.  
  5. #include <sys/param.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <arpa/nameser.h>
  9. #include <ctype.h>
  10. #include <resolv.h>
  11.  
  12. #include "../conf/portability.h"
  13.  
  14. #if !defined(isxdigit)    /* XXX - could be a function */
  15. static int
  16. isxdigit(c)
  17.     register int c;
  18. {
  19.     return ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'F'));
  20. }
  21. #endif
  22.  
  23. static char
  24. xtob(c)
  25.     register int c;
  26. {
  27.     return (c - (((c >= '0') && (c <= '9')) ? '0' : '7'));
  28. }
  29.  
  30. u_int
  31. inet_nsap_addr(ascii, binary, maxlen)
  32.     const char *ascii;
  33.     u_char *binary;
  34.     int maxlen;
  35. {
  36.     register u_char c, nib;
  37.     u_int len = 0;
  38.  
  39.     while ((c = *ascii++) != '\0' && len < maxlen) {
  40.         if (c == '.' || c == '+' || c == '/')
  41.             continue;
  42.         if (!isascii(c))
  43.             return (0);
  44.         if (islower(c))
  45.             c = toupper(c);
  46.         if (isxdigit(c)) {
  47.             nib = xtob(c);
  48.             if (c = *ascii++) {
  49.                 c = toupper(c);
  50.                 if (isxdigit(c)) {
  51.                     *binary++ = (nib << 4) | xtob(c);
  52.                     len++;
  53.                 } else
  54.                     return (0);
  55.             }
  56.             else
  57.                 return (0);
  58.         }
  59.         else
  60.             return (0);
  61.     }
  62.     return (len);
  63. }
  64.  
  65. char *
  66. inet_nsap_ntoa(binlen, binary, ascii)
  67.     int binlen;
  68.     register const u_char *binary;
  69.     register char *ascii;
  70. {
  71.     register int nib;
  72.     int i;
  73.     static char tmpbuf[255*3];
  74.     char *start;
  75.  
  76.     if (ascii)
  77.         start = ascii;
  78.     else {
  79.         ascii = tmpbuf;
  80.         start = tmpbuf;
  81.     }
  82.  
  83.     if (binlen > 255)
  84.         binlen = 255;
  85.  
  86.     for (i = 0; i < binlen; i++) {
  87.         nib = *binary >> 4;
  88.         *ascii++ = nib + (nib < 10 ? '0' : '7');
  89.         nib = *binary++ & 0x0f;
  90.         *ascii++ = nib + (nib < 10 ? '0' : '7');
  91.         if (((i % 2) == 0 && (i + 1) < binlen))
  92.             *ascii++ = '.';
  93.     }
  94.     *ascii = '\0';
  95.     return (start);
  96. }
  97.